package com.jzr.bedside.ui.skill

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.utils.TimeUtils
import com.blankj.utilcode.utils.ToastUtils
import com.jzr.bedside.R
import com.jzr.bedside.base.BaseActivity
import com.jzr.bedside.bean.FeeDataBean
import com.jzr.bedside.bean.FeeRecordListBean
import com.jzr.bedside.presenter.contract.FeeActivityContract
import com.jzr.bedside.presenter.impl.FeeActivityImpl
import com.jzr.bedside.ui.adapter.FeeApadter
import com.jzr.bedside.utils.CommonUtil
import com.jzr.bedside.utils.PreferUtil
import kotlinx.android.synthetic.main.actionbar_root_layout.*
import kotlinx.android.synthetic.main.activity_fee.*
import java.util.*
import kotlin.Comparator
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

class FeeActivity : BaseActivity(), View.OnClickListener, FeeActivityContract.View {

    private val mPresenter = FeeActivityImpl()

    private val dataBeans = mutableListOf<FeeDataBean>()
    private var adapter: FeeApadter? = null

    override fun getLayoutId(): Int {
        return R.layout.activity_fee
    }

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun detachView() {
        mPresenter.detachView()
    }

    override fun initView() {
        ivRight.setOnClickListener(this)
        mPresenter.getFeeRecordList(this,"admissionId",PreferUtil.getInstance().cureNo)
        adapter = FeeApadter(dataBeans)
        rvExpense.adapter = adapter
        val manage = LinearLayoutManager(this)
        manage.orientation = LinearLayoutManager.VERTICAL
        rvExpense.layoutManager = manage

    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ivRight -> {
                finish()
            }
        }
    }

    override fun getFeeRecordListSuccess(data: FeeRecordListBean?) {
        val resultMap: MutableMap<String, MutableList<FeeRecordListBean.DataBean>> =
            HashMap()

        for ((index, item) in data?.data?.withIndex()!!) {
            //该日期已存在，则放入该集合
            val time = TimeUtils.string2Millis(item.feeDate, "yyyy-MM-dd")
            if (resultMap.containsKey(time.toString())) {
                resultMap[time.toString()]?.add(item)
            } else {
                val itemList: MutableList<FeeRecordListBean.DataBean> = ArrayList()
                itemList.add(item)
                resultMap[time.toString()] = itemList
            }
        }

        val feeDataBeanList = mutableListOf<FeeDataBean>()
        for ((key, value) in resultMap) {
            val feeDataBean = FeeDataBean()
            feeDataBean.time = key
            feeDataBean.dataBeanList = value
            var b = 0.0
            for (num in feeDataBean.dataBeanList) {
                b += num.totalAmount
            }
            feeDataBean.totalMoney = CommonUtil.getNoMoreThanTwoDigits(b)
            feeDataBeanList.add(feeDataBean)
        }

        Collections.sort(feeDataBeanList, Comparator<FeeDataBean?> { o1, o2 ->
            if (o1?.time?.toLong()!! < o2?.time?.toLong()!!) {
                return@Comparator 1
            }
            if (o1.time?.toLong()!! == o2.time?.toLong()!!) {
                0
            } else -1
        })
        adapter?.let {
            it.setNewData(feeDataBeanList)
            it.notifyDataSetChanged()
        }
    }

    override fun showError(message: String?) {
        ToastUtils.showShortToast(message)
    }


}
